﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Ex05.MemoryGameLogic
{
    public struct Board
    {
        private int m_BoardHeight;
        private int m_BoardWidth;
        private char[,] m_BoardArray;

        public Board(BoardSize i_BoardSize)
            : this(i_BoardSize.Height, i_BoardSize.Width)
        {
        }

        public Board(int i_BoardHeight, int i_BoardWidth)
        {
            this.m_BoardHeight = i_BoardHeight;
            this.m_BoardWidth = i_BoardWidth;
            this.m_BoardArray = new char[i_BoardHeight, i_BoardWidth];
        }

        public char this[int i_CellRow, int i_CellColumn]
        {
            get { return m_BoardArray[i_CellRow, i_CellColumn]; }
            set { m_BoardArray[i_CellRow, i_CellColumn] = value; }
        }

        public char this[Cell i_Cell]
        {
            get { return m_BoardArray[i_Cell.Row, i_Cell.Column]; }
            set { m_BoardArray[i_Cell.Row, i_Cell.Column] = value; }
        }

        public int NumberOfRows
        {
            get { return this.m_BoardHeight; }
        }

        public int NumberOfColumns
        {
            get { return this.m_BoardWidth; }
        }

        /// <summary>
        /// Fills the board with spaces.
        /// </summary>
        public void FillSpaces()
        {
            for (int i = 0; i < this.m_BoardHeight; i++)
            {
                for (int j = 0; j < this.m_BoardWidth; j++)
                {
                    this.m_BoardArray[i, j] = ' ';
                }
            }
        }

        /// <summary>
        /// Returns the empty cells on the board.
        /// </summary>
        /// <returns>Empty cells on the board.</returns>
        public List<Cell> GetEmptyCells()
        {
            List<Cell> cellsList = new List<Cell>();

            for (int row = 0; row < this.m_BoardHeight; row++)
            {
                for (int col = 0; col < this.m_BoardWidth; col++)
                {
                    if (this.m_BoardArray[row, col] == ' ' || this.m_BoardArray[row, col].ToString() == null || this.m_BoardArray[row, col].ToString() == "\0")
                    {
                        cellsList.Add(new Cell(row, col));
                    }
                }
            }

            return cellsList;
        }

        /// <summary>
        /// Fills the board with random pair values.
        /// </summary>
        public void FillRandomPairCharacters()
        {
            char value = 'A';
            int randCell;
            Random random = new Random();

            List<Cell> emptyCellsList = this.GetEmptyCells();

            while (emptyCellsList != null && emptyCellsList.Count != 0)
            {
                randCell = random.Next(emptyCellsList.Count);
                m_BoardArray[emptyCellsList[randCell].Row, emptyCellsList[randCell].Column] = value;

                emptyCellsList = this.GetEmptyCells();
                randCell = random.Next(emptyCellsList.Count);
                m_BoardArray[emptyCellsList[randCell].Row, emptyCellsList[randCell].Column] = value;

                emptyCellsList = this.GetEmptyCells();

                value++;
            }
        }
    }
}
